package com.atguigu.rabbitmq.server;

import com.atguigu.rabbitmq.utils.RabbitMqutils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 声明主题交换机相关的队列
 *
 * 消费者C2
 */
public class ReceiveLogTopic02 {

    //交换机的名称
    public static final String EXCHANGE_NAME="topic_logs";


    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqutils.getChannel();

     //声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        //声明队列名字
        String queueName="Q2";
        channel.queueDeclare(queueName,false,false,false,null);
        /**
         *
         * 1.队列名字
         *  2.交换机的名字
         *  3.key的值
         */
        channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit");
        channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#");
        System.out.println("等待接受消息");

        DeliverCallback deliverCallback= (var1,var2)->{
            System.out.println(new String(var2.getBody(),"utf-8"));
            System.out.println("接受队列"+queueName+"绑定建"+var2.getEnvelope().getRoutingKey());
        };

        //接受消息
        channel.basicConsume(queueName,true,deliverCallback,var1->{});


    }

}
